// pages/bankinfo/bankinfo.js
var app = getApp();
var com = require('../../common.js')
Page({
  data: {
    phoneNum: '获取验证码',
    bindEndClick: 'clickEnd',
    number: 61,
    imageCode: '',
    imageImg: '',
    code: '',
    phoneKey: '',
    numberKey: '',
    showuum: true,
    show: true,
    supplier: '',
    pass: '',
    password: '',
    account_number: '',
    hidshow: false,
    account_name: '',
    account_bank: '',
    phoneNumber: '',
    panduanyzm: ''
  },
  onLoad: function(options) {
    wx.setStorageSync('functionId', 3)
    var _this = this
    _this.setData({
      phoneNumber: wx.getStorageSync('phone')
    })
    _this.onObtain()
  },
  onReady: function() {},
  onShow: function() {
    var _this = this
    _this.supplier()
  },
  onHide: function() {},
  onUnload: function() {},
  onPullDownRefresh: function() {},
  onReachBottom: function() {},
  onShareAppMessage: function() {},
  //刷新验证码
  f5codeImg() {
    var _ = this
    _.onObtain()
  },
  //获取验证码
  onObtain() {
    var _this = this,
      url = app.link.obtain,
      data = {}
    com.link(url, data, function(rep) {
      console.log(rep)
      _this.setData({
        imageCode: rep.data.response.captcha.key,
        imageImg: rep.data.response.captcha.img,
      })
    }, 'GET')
  },
  //获取输入的手机号
  phoneNumber(e) {
    var _this = this,
      id = e.currentTarget.id
    if (id == 1) {
      _this.setData({
        account_name: e.detail.value
      })
    } else if (id == 2) {
      _this.setData({
        account_bank: e.detail.value,
        listshow: false,
        hidshow: false
      })
    } else if (id == 3) {
      _this.setData({
        account_number: e.detail.value
      })
    } else if (id == 4) {
      if (_this.data.account_number != e.detail.value && _this.data.account_number != '') {
        console.log(_this.data.account_number, e.detail.value)
        wx.showToast({
          title: '输入不一致',
          icon: 'none'
        })
        _this.setData({
          showuum: false,
          account_: e.detail.value
        })
      } else {
        _this.setData({
          showuum: true,
          account_: e.detail.value
        })
      }
    } else if (id == 5) {
      _this.setData({
        phoneNumber: e.detail.value
      })
    } else if (id == 6) {
      _this.setData({
        code: e.detail.value
      })
    } else if (id == 7) {
      _this.setData({
        numberKey: e.detail.value
      })
    } else if (id == 8) {
      _this.setData({
        password: e.detail.value
      })
    } else if (id == 9) {
      if (_this.data.password != e.detail.value && _this.data.password != '') {
        console.log(_this.data.password, e.detail.value)
        wx.showToast({
          title: '输入不一致',
          icon: 'none'
        })
        _this.setData({
          show: false,
          pass: e.detail.value
        })
      } else {
        _this.setData({
          show: true,
          pass: e.detail.value
        })
      }
    }
  },
  clicklist(e) {
    var _this = this
    _this.setData({
      account_bank: e.currentTarget.id,
      listshow: false,
      hidshow: false
    })
  },
  searchList() {
    var _this = this
    setTimeout(() => {
      com.link(app.link.banksearch, {
        search: _this.data.account_bank
      }, function(rep) {
        _this.setData({
          list: rep.data.data,
          hidshow: true
        })
      }, 'GET')
    }, 200)

  },
  inputFocus() {
    var _this = this
    console.log('获取焦点')
    _this.setData({
      listshow: true
    })
  },
  //点击倒计时
  clickEnd() {
    wx.showToast({
      icon: 'loading',
      title: '发送中',
    })
    var _this = this,
      data = {
        captcha: _this.data.code,
        phone_number: _this.data.phoneNumber,
        key: _this.data.imageCode,
      },
      url = app.link.basics

    com.link(url, data, function(rep) {
      console.log(rep.data.response.captcha)
      if (rep.data.state == false) {
        com.toast(rep.data.message)

      } else {
        com.toast(rep.data.message)
        console.log(rep.data.response.key)
        _this.setData({
          phoneKey: rep.data.response.key,
          panduanyzm: rep.data.response.captcha
        })
        console.log(_this.data.phoneKey)
        wx.showToast({
          title: '发送成功',
        })
        _this.onSetTimeout()
      }
    })
  },
  //倒计时
  onSetTimeout() {
    var _this = this
    var numberNum = parseInt(_this.data.number)
    numberNum = numberNum - 1
    if (_this.data.number == 0) {
      _this.setData({
        phoneNum: "重新获取",
        bindEndClick: 'clickEnd',
        number: 61
      })
      return false
    } else {
      _this.setData({
        phoneNum: numberNum + '秒后重新获取',
        bindEndClick: '',
        number: numberNum
      })
    }
    setTimeout(() => {
      _this.onSetTimeout()
    }, 1000)
  },
  //获取合同内容
  supplier() {
    var _this = this,
      urls = app.link.appraisal,
      data = {}
    com.link(urls, data, function(rep) {
      console.log(rep)
      var url = app.link.supplier + rep.data.response.response.supplier + '.st'
      com.link(url, {}, function(res) {
        console.log(res.data.response)
        wx.getImageInfo({
          src: res.data.response.contract.contract,
          success(e) {
            console.log(e)
            _this.setData({
              supplier: rep.data.response.response.supplier
            })
          }
        })
      }, 'GET')
    }, 'GET')
  },
  next() {
    var _this = this
    if (_this.data.account_name == '') {
      wx.showToast({
        title: '请填写开户名称',
        icon: 'none'
      })
      return false;
    } else if (_this.data.account_bank == '') {
      wx.showToast({
        title: '请填写开户银行',
        icon: 'none'
      })
      return false;
    } else if (_this.data.account_ != _this.data.account_number) {
      wx.showToast({
        title: '两次银行账号不同',
        icon: 'none'
      })
      return false;
    } else if (_this.data.account_number == '') {
      wx.showToast({
        title: '请填写银行账号',
        icon: 'none'
      })
      return false;
    } else if (_this.data.password == '') {
      wx.showToast({
        title: '请填写资金密码',
        icon: 'none'
      })
      return false;
    } else if (_this.data.password != _this.data.pass) {
      wx.showToast({
        title: '两次密码不一样',
        icon: 'none'
      })
      return false;
    }
    else{
      if (_this.data.numberKey != _this.data.panduanyzm) {
        wx.showToast({
          title: '请输入正确的手机验证码',
          icon: 'none'
        })
        return false;
      }
    }
    let data = {
      account_name: _this.data.account_name,
      account_bank: _this.data.account_bank,
      account_number: _this.data.account_number,
      account_password: _this.data.password,
      key: _this.data.phoneKey,
      captcha: _this.data.numberKey
    }
    wx.navigateTo({
      url: '/pages/contractdetails/contractdetails?info=' + JSON.stringify(data),
    })
    // url = app.link.signingsupplier + this.data.supplier + '.st'
    // com.link(url, {}, function(rep) {
    //   console.log(rep)
    //   if (rep.data.state) {
    //     setTimeout(() => {
    //       wx.navigateTo({
    //         url: '/pages/contractdetails/contractdetails',
    //       })
    //     }, 1500)
    //   } else {
    //     com.toast(rep.data.message)
    //   }
    // }, "GET")
  }
})